package cn.hiapi.core.basic.pageable;


import org.springframework.data.domain.AbstractPageRequest;
import org.springframework.data.domain.Sort;

/**
 * @author AdinZ
 * @date 2020/11/10 15:10
 */
public class PageRequest extends AbstractPageRequest {
    private final Sort sort;


    private PageRequest(int page, int size) {
        this(page, size, Sort.unsorted());
    }
    private PageRequest(int page,int size,Sort sort) {
        super(page - 1, size);
        this.sort = sort;
    }




    @Override
    public PageRequest next() {
        return new PageRequest(this.getPageNumber() + 1, this.getPageSize(), this.getSort());
    }

    @Override
    public PageRequest previous() {
        return this.getPageNumber() == 0 ? this : new PageRequest(this.getPageNumber() - 1, this.getPageSize(), this.getSort());

    }

    @Override
    public PageRequest first() {
        return new PageRequest(0, this.getPageSize(), this.getSort());
    }

    @Override
    public PageRequest withPage(int pageNumber) {
        return new PageRequest(pageNumber, this.getPageSize(), this.getSort());
    }


    public static PageRequest of(int page, int size, Sort.Direction direction, String... properties) {
        return of(page, size,Sort.by(direction, properties));
    }

    public static PageRequest of(int page, int size, Sort sort) {
        return new PageRequest(page,size,sort);
    }
    public static PageRequest of(int page, int size) {
        return new PageRequest(page,size,Sort.unsorted());
    }

    @Override
    public Sort getSort() {
        return this.sort;
    }
}

